/**
 * 
 */
package com.idea.zd.sys.controller;

import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.idea.zd.sys.model.Admin;
import com.idea.zd.sys.model.Permission;
import com.idea.zd.sys.service.PermissionService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/per",viewPath="/pages")
public class PermissionController extends BaseController {
	private static final Logger log = Logger.getLogger(PermissionController.class);
	@Service
	private PermissionService permissionService;
	
	/**
	 * 跳转到权限页
	 */
	@RequiresPermissions("per/toPermission")
	public void toPermission(){
		getNavigation();
		render("permission/permission-list.html");
	}
	
	/**
	 * 添加权限页
	 */
	@RequiresPermissions("per/toAddPer")
	public void toAddPer(){
		Permission permission = new Permission();
		
		if(!StrKit.isBlank(getPara("id"))){
			permission = permissionService.getById(this.getParaToInt("id"));
		}
		setAttr("permission",permission );
		render("permission/permission-edit.html");
	}

	@RequiresPermissions("per/toEditPer")
	public void toEditPer(){
		Permission permission = new Permission();

		if(!StrKit.isBlank(getPara("id"))){
			permission = permissionService.getById(this.getParaToInt("id"));
		}
		setAttr("permission",permission );
		render("permission/permission-edit.html");
	}
	
	/**
	 * 跳转到子级权限页
	 */
	@RequiresPermissions("per/toChildPer")
	public void toChildPer(){
		Permission permission = permissionService.getById(this.getParaToInt("id"));
		setAttr("perName", permission.get("permission_name"));
		setAttr("perId", permission.get("id"));
		render("permission/permission-children-list.html");
	}
	
	/**
	 * 跳转到子级添加权限页
	 */
	@RequiresPermissions("per/toChildAddPer")
	public void toChildAddPer(){
		Permission permission = new Permission();
		setAttr("parentId",this.getPara("parentId"));
		setAttr("permission",permission );
		render("permission/permission-children-edit.html");
	}

	/**
	 * 跳转到子级编辑权限页
	 */
	@RequiresPermissions("per/toChildEditPer")
	public void toChildEditPer(){
		Permission permission = new Permission();

		if(!StrKit.isBlank(getPara("id"))){
			permission = permissionService.getById(this.getParaToInt("id"));
		}

		setAttr("parentId",this.getPara("parentId"));
		setAttr("permission",permission );
		render("permission/permission-children-edit.html");
	}

	/**
	 * 跳转到权限功能页
	 */
	@RequiresPermissions("per/toFunction")
	public void toFunction(){
		Permission permission = permissionService.getById(this.getParaToInt("id"));
		setAttr("perName", permission.get("permission_name"));
		setAttr("perId", permission.get("id"));
		render("permission/permission-function-list.html");
	}

	/**
	 * 跳转到功能添加页
	 */
	public void toFunctionEdit(){
		Permission permission = new Permission();

		if(!StrKit.isBlank(getPara("id"))){
			permission = permissionService.getById(this.getParaToInt("id"));
		}

		setAttr("parentId",this.getPara("parentId"));
		setAttr("permission",permission );
		render("permission/permission-function-edit.html");
	}
	
	/**
	 * 获取父级列表
	 */
	public void getPerList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		super.setPage(permissionService.getPage(curPage));
	}
	
	/**
	 * 获取子级列表
	 */
	public void getChildrePerList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		super.setPage(permissionService.getChildrePage(curPage,this.getParaToInt("id")));
	}
	
	/**
	 * 添加权限
	 */
	public void addPer(){
		Admin admin = (Admin) getSession().getAttribute("admin");
		Permission permission = new Permission();
		permission.set("permission_name",this.getPara("perName") );
		permission.set("visit_url", this.getPara("perUrl"));
		permission.set("icon", this.getPara("perIcon"));
		permission.set("parent_id", this.getPara("parentId"));
		permission.set("create_name", admin.get("name"));
		permission.set("permission_type", this.getPara("perType"));
		permission.set("create_date", new Date());
		permission.set("update_name", admin.get("name"));
		
		if(permissionService.add(permission)){
			msgAdd();
		}
		
	}
	
	/**
	 * 编辑权限
	 */
	public void editPer(){
		Admin admin = (Admin) getSession().getAttribute("admin");
		
		Permission permission = new Permission();
		permission.set("id",this.getPara("id") );
		permission.set("permission_name",this.getPara("perName") );
		permission.set("visit_url", this.getPara("perUrl"));
		permission.set("icon", this.getPara("perIcon"));
		permission.set("parent_id", this.getPara("parentId"));
		permission.set("create_name", admin.get("name"));
		permission.set("create_date", new Date());
		permission.set("update_name", admin.get("name"));
		
		if(permissionService.update(permission)){
			msgAdd();
		}
		
	}
	
	/**
	 * 删除权限
	 */
	@RequiresPermissions("per/delPer")
	public void delPer(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			permissionService.del(Integer.parseInt(id));
		}
		
		msgSuccess();
	}
	
	/**
	 * 验证标识
	 */
	public void validateUrl(){
		
		if(permissionService.validateUrl(this.getParaToInt("parentId"), this.getPara("id"), this.getPara("perUrl"))){
			msgSuccess();
		}else{
			super.msgSetVal("该标识已存在");
		}
	}
	
	
	/**
	 * 更新排序
	 */
	public void setSort(){
		
		if(permissionService.setSort(this.getParaToInt("firstId"), this.getParaToInt("secondId"), this.getParaToInt("firstSort"), this.getParaToInt("secondSort"))){
			msgSuccess();
		}
	}


	/**
	 * 获取树形权限
	 */
	public void getTreePermission(){

		super.msgSuccess(permissionService.getTreePermission(this.getParaToInt("roleId")));
	}
	
}
